package com.good.cardview

import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import android.widget.LinearLayout
import androidx.annotation.IntRange
import androidx.annotation.NonNull
import androidx.annotation.Nullable

/**
 * date        ：2020/8/25
 * author      ：蒙景博
 * description ：
 */
class GoodCardLayout : LinearLayout, GoodCardLayoutDelegate {

    private var mLayoutHelper: GoodCardLayoutHelper

    constructor(@NonNull context: Context) : this(context, null)
    constructor(@NonNull context: Context, @Nullable attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(
        @NonNull context: Context,
        @Nullable attrs: AttributeSet?,
        defStyleAttr: Int
    ) : this(context, attrs, defStyleAttr, 0)

    constructor(
        @NonNull context: Context,
        @Nullable attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes) {
        mLayoutHelper = GoodCardLayoutHelper(
            context,
            attrs,
            defStyleAttr,
            this
        )
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val mWidthMeasureSpec = mLayoutHelper.getMeasuredWidthSpec(widthMeasureSpec)
        val mHeightMeasureSpec = mLayoutHelper.getMeasuredHeightSpec(heightMeasureSpec)
        super.onMeasure(mWidthMeasureSpec, mHeightMeasureSpec)
        val mMinWidth = mLayoutHelper.handleMiniWidth(widthMeasureSpec, measuredWidth)
        val mMinHeight = mLayoutHelper.handleMiniHeight(heightMeasureSpec, measuredHeight)
        if (widthMeasureSpec != mMinWidth || heightMeasureSpec != mMinHeight) {
            super.onMeasure(mMinWidth, mMinHeight)
        }
    }

    override fun dispatchDraw(canvas: Canvas?) {
        super.dispatchDraw(canvas)
        mLayoutHelper.drawDividers(canvas, width, height)
        mLayoutHelper.dispatchRoundBorderDraw(canvas)
    }

    override fun setWidthLimit(widthLimit: Int): Boolean {
        if (mLayoutHelper.setWidthLimit(widthLimit)) {
            requestLayout()
            postInvalidate()
        }
        return true
    }

    override fun setHeightLimit(heightLimit: Int): Boolean {
        if (mLayoutHelper.setHeightLimit(heightLimit)) {
            requestLayout()
            postInvalidate()
        }
        return true
    }

    override fun setOutlineInset(left: Int, top: Int, right: Int, bottom: Int) {
        mLayoutHelper.setOutlineInset(left, top, right, bottom)
    }

    override fun setOutlineExcludePadding(outlineExcludePadding: Boolean) {
        mLayoutHelper.setOutlineExcludePadding(outlineExcludePadding)
    }

    override fun updateLeftDivider(
        insetTop: Int,
        insetBottom: Int,
        dividerWidth: Int,
        dividerColor: Int
    ) {
        mLayoutHelper.updateLeftDivider(insetTop, insetBottom, dividerWidth, dividerColor)
        postInvalidate()
    }

    override fun updateTopDivider(
        insetLeft: Int,
        insetRight: Int,
        dividerHeight: Int,
        dividerColor: Int
    ) {
        mLayoutHelper.updateTopDivider(insetLeft, insetRight, dividerHeight, dividerColor)
        postInvalidate()
    }

    override fun updateRightDivider(
        insetTop: Int,
        insetBottom: Int,
        dividerWidth: Int,
        dividerColor: Int
    ) {
        mLayoutHelper.updateRightDivider(insetTop, insetBottom, dividerWidth, dividerColor)
        postInvalidate()
    }

    override fun updateBottomDivider(
        insetLeft: Int,
        insetRight: Int,
        dividerHeight: Int,
        dividerColor: Int
    ) {
        mLayoutHelper.updateBottomDivider(insetLeft, insetRight, dividerHeight, dividerColor)
        postInvalidate()
    }

    override fun onlyShowLeftDivider(
        insetTop: Int,
        insetBottom: Int,
        dividerWidth: Int,
        dividerColor: Int
    ) {
        mLayoutHelper.onlyShowLeftDivider(insetTop, insetBottom, dividerWidth, dividerColor)
    }

    override fun onlyShowTopDivider(
        insetLeft: Int,
        insetRight: Int,
        dividerHeight: Int,
        dividerColor: Int
    ) {
        mLayoutHelper.onlyShowTopDivider(insetLeft, insetRight, dividerHeight, dividerColor)
        postInvalidate()
    }

    override fun onlyShowRightDivider(
        insetTop: Int,
        insetBottom: Int,
        dividerWidth: Int,
        dividerColor: Int
    ) {
        mLayoutHelper.onlyShowRightDivider(insetTop, insetBottom, dividerWidth, dividerColor)
        postInvalidate()
    }

    override fun onlyShowBottomDivider(
        insetLeft: Int,
        insetRight: Int,
        dividerHeight: Int,
        dividerColor: Int
    ) {
        mLayoutHelper.onlyShowBottomDivider(insetLeft, insetRight, dividerHeight, dividerColor)
        postInvalidate()
    }

    override fun setLeftDividerAlpha(@IntRange(from = 0, to = 255) dividerAlpha: Int) {
        mLayoutHelper.setLeftDividerAlpha(dividerAlpha)
        postInvalidate()
    }

    override fun setTopDividerAlpha(@IntRange(from = 0, to = 255) dividerAlpha: Int) {
        mLayoutHelper.setTopDividerAlpha(dividerAlpha)
        postInvalidate()
    }

    override fun setRightDividerAlpha(@IntRange(from = 0, to = 255) dividerAlpha: Int) {
        mLayoutHelper.setRightDividerAlpha(dividerAlpha)
        postInvalidate()
    }

    override fun setBottomDividerAlpha(@IntRange(from = 0, to = 255) dividerAlpha: Int) {
        mLayoutHelper.setBottomDividerAlpha(dividerAlpha)
        postInvalidate()
    }

    override var radius: Int
        get() = mLayoutHelper.radius
        set(value) {
            mLayoutHelper.radius = value
        }

    override fun setRadius(radius: Int, hideRadiusSide: Int) {
        mLayoutHelper.setRadius(radius, hideRadiusSide)
    }

    override var hideRadiusSide: Int
        get() = mLayoutHelper.hideRadiusSide
        set(value) {
            mLayoutHelper.hideRadiusSide = value
        }

    override fun setRadiusAndShadow(radius: Int, shadowElevation: Int, shadowAlpha: Float) {
        mLayoutHelper.setRadiusAndShadow(radius, shadowElevation, shadowAlpha)
    }

    override fun setRadiusAndShadow(
        radius: Int,
        hideRadiusSide: Int,
        shadowElevation: Int,
        shadowAlpha: Float
    ) {
        mLayoutHelper.setRadiusAndShadow(radius, hideRadiusSide, shadowElevation, shadowAlpha)
    }

    override fun setRadiusAndShadow(
        radius: Int,
        hideRadiusSide: Int,
        shadowElevation: Int,
        shadowColor: Int,
        shadowAlpha: Float
    ) {
        mLayoutHelper.setRadiusAndShadow(
            radius,
            hideRadiusSide,
            shadowElevation,
            shadowColor,
            shadowAlpha
        )
    }

    override fun hasBorder(): Boolean = mLayoutHelper.hasBorder()

    override fun setBorderWidth(borderWidth: Int) {
        mLayoutHelper.setBorderWidth(borderWidth)
        postInvalidate()
    }

    override fun setBorderColor(borderColor: Int) {
        mLayoutHelper.setBorderColor(borderColor)
        postInvalidate()
    }

    override var shadowColor: Int
        get() = mLayoutHelper.shadowColor
        set(value) {
            mLayoutHelper.setBorderColor(value)
        }

    override var shadowAlpha: Float
        get() = mLayoutHelper.shadowAlpha
        set(value) {
            mLayoutHelper.shadowAlpha = value
        }

    override var shadowElevation: Int
        get() = mLayoutHelper.shadowElevation
        set(value) {
            mLayoutHelper.shadowElevation = value
        }

    override fun hasLeftSeparator(): Boolean = mLayoutHelper.hasLeftSeparator()

    override fun hasTopSeparator(): Boolean = mLayoutHelper.hasTopSeparator()

    override fun hasRightSeparator(): Boolean = mLayoutHelper.hasRightSeparator()

    override fun hasBottomSeparator(): Boolean = mLayoutHelper.hasBottomSeparator()

    override fun updateLeftSeparatorColor(color: Int) {
        mLayoutHelper.updateLeftSeparatorColor(color)
    }

    override fun updateTopSeparatorColor(color: Int) {
        mLayoutHelper.updateTopSeparatorColor(color)
    }

    override fun updateRightSeparatorColor(color: Int) {
        mLayoutHelper.updateRightSeparatorColor(color)
    }

    override fun updateBottomSeparatorColor(color: Int) {
        mLayoutHelper.updateBottomSeparatorColor(color)
    }
}